/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view.list;

import java.util.LinkedList;

/**
 *
 * @author Walter Lütgenau
 */
public class XPathParser {

    public static final String TYPE_CONSTRAINT = "element(";
    public static final String VALUE_CONSTRAINT = "[";
    
    String query;

    public XPathParser(String query) {
        this.query = query;
    }
    
    /**
     * Return the path component of the query.
     * 
     * @return The path component
     */
    public String getPath() {
        int start = query.indexOf(TYPE_CONSTRAINT);
        if (start >= 0)
            return query.substring(0, start);
        
        start = query.indexOf(VALUE_CONSTRAINT);
        if (start >= 0)
            return query.substring(0, start);
        
        return query;
    }
    
    /**
     * Return the type constraint of the query.
     * 
     * @return the type constraint
     */
    public String getTypeConstraint() {
        int start = query.indexOf(TYPE_CONSTRAINT);
        if ( start < 0)
            return null;
        
        start = query.indexOf(",", start);
        int end = query.indexOf(")", start);
        if ( start < 0 || end < 0 || start >= end)
            return null;
        
        return query.substring(start+1, end).trim();
    }

    /**
     * Return the column specifier of the query.
     * 
     * @return the column specifier
     */
    public String[] getColumnSpecifier() {
        String[] result = null;

        int endPropertyConstraint = query.indexOf("]");
        int start = -1;
        int end = -1;

        if (endPropertyConstraint > 0)
            start = query.indexOf("(", endPropertyConstraint);
        else
            start = query.lastIndexOf("(");

        if (start > 0) {
            String columnSpecifier = query.substring(start + 1, query.indexOf(")", start));
            if (columnSpecifier.trim().startsWith("@")) { // columnSpecifier starts with an attribute.
                result = columnSpecifier.split("\\|");
                for (int i = 0; i < result.length; i++) {
                    String column = result[i];
                    column = column.trim();
                    if ( column.length() == 0)
                        continue;
                    result[i] = column.substring(1);
                }
            }
        }

        return result;
    }
    
    public static void main(String[] arguments) {
        XPathParser parser = new XPathParser("/jcr:root/Project/Software development//element(*, nodes:accessControl) [jcr:contains(@nodes:text, 'nodes')/(@jcr:path )");
        String result = parser.getPath();
    }
}
